<?php namespace nimo\domains\master\controllers;

use BaseController;
use Input;
use View;
use Validator;
use Redirect;
use Request;
use Response;

use nimo\domains\master\models\Domain as Domain;

class DomainController extends BaseController {

    /**
     * Domain Repository
     *
     * @var Domain
     */
    protected $domain;

    public function __construct(Domain $domain)
    {
        $this->domain = $domain;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $domains = Domain::orderBy('node_left')->get();
        if (Request::ajax()) {
            return Response::json($domains);
        }
        $nested = array_map(function($domain){ return $domain->toArray();}, $domains->all());
        $jsonDomain = json_encode($nested);
        return View::make('nimo.domains.master.domain.index', compact('domains', 'jsonDomain'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('nimo.domains.master.domain.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();
        $validation = Validator::make($input, Domain::$rules);

        if ($validation->passes())
        {
            $domain = null;
            $parentid = Input::get('node_parent');
            if ($parentid > 0 ) {
                $parent = Domain::find($parentid);
                if ($parent != null) {
                    $data = Input::only('id', 'name');
                    $domain = $this->domain->create($input);
                    $domain->makeChildOf($parent);
                }
            } else {
                $domain = $this->domain->create($input);
            }
            if (!is_null($domain)) {
                return Redirect::route('nd.master.domain.show', $domain->id)
                    ->with('success', 'There were successed.');
            }
            return Redirect::route('nd.master.domain.create')
                ->withInput()
                ->with('error', 'There were node relation errors.');
        }
        return Redirect::route('nd.master.domain.create')
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $domain = $this->domain->findOrFail($id);

        return View::make('nimo.domains.master.domain.show', compact('domain'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $domain = $this->domain->find($id);

        if (is_null($domain))
        {
            return Redirect::route('nd.master.domain.index');
        }

        return View::make('nimo.domains.master.domain.edit', compact('domain'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $input = array_except(Input::all(), '_method');
        $validation = Validator::make($input, Domain::$rules);

        if ($validation->passes())
        {
            $domain = $this->domain->find($id);
            $parentid = Input::get('node_parent');
            $domain->update($input);
            if (!empty($parentid) && $domain->node_parent != $parentid) {
                // change parent
                $domain->makeChildOf($parentid);
            } else if(empty($parentid) && !empty($domain->node_parent)){
                // root
                $domain->makeRoot();
            }
            return Redirect::route('nd.master.domain.show', $id)
                ->with('success', 'There were successed.');
        }

        return Redirect::route('nd.master.domain.edit', $id)
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $this->domain->find($id)->delete();

        return Redirect::route('nd.master.domain.index');
    }

    /**
     * move up
     *
     * @param  int  $id
     * @return Response
     */
    public function up($id)
    {
        $domain = $this->domain->find($id);
        if (!is_null($domain)) {
            $node = $domain->getLeftSibling();
            if (!is_null($node)) {
                $domain =  $domain->moveToLeftOf($node);
                return Redirect::route('nd.master.domain.index')
                    ->with('success', 'There were successed.');
            }
        }

        return Redirect::route('nd.master.domain.index');
    }

    /**
     * move down
     *
     * @param  int  $id
     * @return Response
     */
    public function down($id)
    {
        $domain = $this->domain->find($id);
        if (!is_null($domain)) {
            $node = $domain->getRightSibling();
            if (!is_null($node)) {
                $domain =  $domain->moveToRightOf($node);
                return Redirect::route('nd.master.domain.index')
                    ->with('success', 'There were successed.');
            }
        }

        return Redirect::route('nd.master.domain.index');
    }

}